package com.situ.onlinebookstore.front.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.onlinebookstore.common.entity.Category;
import com.situ.onlinebookstore.common.mapper.CategoryMapper;
import com.situ.onlinebookstore.front.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;

    @Override
    public List get(Category category) {
        category.setStatus(0);
        List<Map<String, Object>> list = categoryMapper.select(category);
        //遍历list
        list.forEach((item) -> {
            if(item.get("parent_id").equals(0)){
                //当前分类是一个一级分类
                //查询当前分类下的二级分类
                Category where = new Category();
                where.setParentId((Integer) item.get("id"));
                where.setStatus(0);

                //交给item
                item.put("children",categoryMapper.select(where));

            }
        });
        return list;
    }

    @Override
    public PageInfo getForPage(Integer page, Integer limit, Category category) {
        category.setStatus(0);
        PageHelper.startPage(page, limit);
        List list = get(category);
        return new PageInfo(list);
    }
}
